package com.itheima.resp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/addStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
//        Student stu=new Student();
       Student stu=new Student();
       stu.setName(name);
       stu.setAge(Integer.parseInt(age));
       stu.setScore(Integer.parseInt(score));
        BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\stu.txt",true));
        bw.write(stu.getName()+","+stu.getAge()+","+stu.getScore());
        bw.newLine(); 
        bw.close();
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("添加成功，1秒后跳转到首页");
        resp.setHeader("Refresh","2;URL=/stu/index.html");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
